package com.kgc.storeprovider.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.store.pojo.*;
import com.kgc.storeprovider.dao.StoreGoodSummaryMapper;
import com.kgc.storeprovider.service.StoreGoodSummaryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StoreGoodSummaryServiceImpl implements StoreGoodSummaryService {

    private Logger logger = LoggerFactory.getLogger(StoreGoodSummaryServiceImpl.class);

    @Autowired
    private StoreGoodSummaryMapper storeGoodSummaryMapper;

    @Override
    public ResponseMsg selectSummaryByStoreGood(Map<String, Object> map) {
        logger.info("StoreGoodSummaryServiceImpl selectSummaryByStoreGood map:{}", map);
        ResponseMsg msg = new ResponseMsg();
        PageInfo<StoreGoodSummary> pageInfo = new PageInfo<>();
        pageInfo.setPageNo(map.get("pageNo") == null ? 1 : Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString()));
        Page<Object> page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        List<StoreGoodSummary> storeGoodSummaryList = storeGoodSummaryMapper.selectSummaryByStoreGood(map);
        pageInfo.setList(storeGoodSummaryList);
        pageInfo.setTotal((int) page.getTotal());
        msg.setData(pageInfo);
        logger.debug("StoreGoodSummaryServiceImpl selectSummaryByStoreGood result:{}", msg);
        return msg;
    }

    @Override
    public ResponseMsg selectAll() {
        logger.info("StoreGoodSummaryServiceImpl selectAll is start...");
        ResponseMsg msg = new ResponseMsg();
        List<KcStore> kcStoreList = storeGoodSummaryMapper.selectAll();
        if (kcStoreList == null || kcStoreList.isEmpty()) {
            msg.setCode("301");
            msg.setMsg("获取仓库信息失败");
            return msg;
        }
        msg.setCode("200");
        msg.setMsg("获取仓库信息成功");
        msg.setData(kcStoreList);
        return msg;
    }

    @Override
    public ResponseMsg selectSummaryByGoodsTransactionDetail(Map<String, Object> map) {
        logger.info("StoreGoodSummaryServiceImpl selectSummaryByGoodsTransactionDetail map:{}", map);
        ResponseMsg msg = new ResponseMsg();
        PageInfo<GoodsTransactionDetail> pageInfo = new PageInfo<>();
        pageInfo.setPageNo(map.get("pageNo") == null ? 1 : Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString()));
        Page<Object> page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        List<GoodsTransactionDetail> goodsTransactionDetailList = storeGoodSummaryMapper.selectSummaryByGoodsTransactionDetail(map);
        pageInfo.setList(goodsTransactionDetailList);
        pageInfo.setTotal((int) page.getTotal());
        msg.setData(pageInfo);
        logger.debug("StoreGoodSummaryServiceImpl selectSummaryByGoodsTransactionDetail result:{}", msg);
        return msg;
    }

    @Override
    public ResponseMsg selectSummaryByBatchTrackingDetail(Map<String, Object> map) {
        logger.info("StoreGoodSummaryServiceImpl selectSummaryByBatchTrackingDetail map:{}", map);
        ResponseMsg msg = new ResponseMsg();
        PageInfo<BatchTrackingDetail> pageInfo = new PageInfo<>();
        pageInfo.setPageNo(map.get("pageNo") == null ? 1 : Integer.parseInt(map.get("pageNo").toString()));
        pageInfo.setPageSize(map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString()));
        Page<Object> page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        List<BatchTrackingDetail> batchTrackingDetailList = storeGoodSummaryMapper.selectSummaryByBatchTrackingDetail(map);
        pageInfo.setList(batchTrackingDetailList);
        pageInfo.setTotal((int) page.getTotal());
        msg.setData(pageInfo);
        logger.debug("StoreGoodSummaryServiceImpl selectSummaryByBatchTrackingDetail result:{}", msg);
        return msg;
    }

//    @Override
//    public ResponseMsg updateStore(int gid, int sid, int stock) {
//        logger.info("StoreGoodSummaryServiceImpl updateStore gid:{}", gid);
//        ResponseMsg msg = new ResponseMsg();
//        int i = storeGoodSummaryMapper.updateStore(gid, sid, stock);
//        if (i > 0) {
//            msg.setCode("200");
//            msg.setMsg("更新库存成功");
//            return msg;
//        }
//        msg.setCode("301");
//        msg.setMsg("更新库存失败");
//        return msg;
//    }

    @Override
    public ResponseMsg method(Map<String, Object> map) {
        logger.info("StoreGoodSummaryServiceImpl method map:{}", map);
        ResponseMsg msg = new ResponseMsg();
        int stock = (int) map.get("stock");
        int sumCost = (int) map.get("sumCost");
        int sid = (int) map.get("sid");
        int gid = (int) map.get("gid");
        if (stock > 0) {    //入库
            KcStoreGood kcStoreGood = storeGoodSummaryMapper.selectKcStoreGoodBySidCid(sid, gid);
            if (kcStoreGood != null) {
                int i = storeGoodSummaryMapper.updateStore(gid, sid, stock, BigDecimal.valueOf(sumCost));
                if (i > 0) {
                    msg.setCode("200");
                    msg.setMsg("修改库存成功");
                } else {
                    msg.setCode("301");
                    msg.setMsg("修改库存失败");
                }
                return msg;
            } else {
                KcStoreGood good = new KcStoreGood();
                good.setSingleCost(BigDecimal.valueOf(sumCost / stock));
                good.setSumCost(BigDecimal.valueOf(sumCost));
                good.setStock(stock);
                good.setSid(sid);
                good.setGid(gid);
                good.setCreateDate(new Date());
                good.setUpdateDate(new Date());

                int i = storeGoodSummaryMapper.insertKcStoreGood(good);
                if (i > 0) {
                    msg.setCode("200");
                    msg.setMsg("新增库存信息成功");
                } else {
                    msg.setCode("301");
                    msg.setMsg("新增库存信息失败");
                }
                return msg;
            }
        } else {    //出库
            KcStoreGood kcStoreGood = storeGoodSummaryMapper.selectKcStoreGoodBySidCid(sid, gid);
            if (kcStoreGood == null) {
                msg.setCode("302");
                msg.setMsg("未查询到库存信息");
                return msg;
            } else {
                int i = storeGoodSummaryMapper.updateStore(gid, sid, stock, BigDecimal.valueOf(sumCost));
                if (i > 0) {
                    msg.setCode("200");
                    msg.setMsg("修改库存成功");
                } else {
                    msg.setCode("301");
                    msg.setMsg("修改库存失败");
                }
                return msg;
            }
        }
    }

    @Override
    public ResponseMsg selectKcStoreGoodBySidCid(int sid, int gid) {
        logger.info("StoreGoodSummaryServiceImpl selectKcStoreGoodBySidCid sid:{},gid:{}", sid, gid);
        ResponseMsg msg = new ResponseMsg();
        KcStoreGood kcStoreGood = storeGoodSummaryMapper.selectKcStoreGoodBySidCid(sid, gid);
        if (kcStoreGood == null) {
            msg.setCode("301");
            msg.setMsg("未查询到相关信息");
            return msg;
        }
        msg.setCode("200");
        msg.setData(kcStoreGood);
        return msg;
    }

    @Override
    public ResponseMsg getTotalInventoryCost() {
        logger.info("StoreGoodSummaryServiceImpl getTotalInventoryCost is start...");
        ResponseMsg msg = new ResponseMsg();
        Map<String, Object> totalInventoryCostMap = storeGoodSummaryMapper.getTotalInventoryCost();
        if (totalInventoryCostMap.isEmpty()) {
            msg.setCode("301");
            msg.setMsg("未查询到相关信息");
            return msg;
        }
        msg.setCode("200");
        msg.setData(totalInventoryCostMap);
        return msg;
    }
}
